// 和用户状态管理相关
import { createSlice } from "@reduxjs/toolkit";
import { setToken as N_setToken, getToken, removeToken } from "../../utils";

const userStore = createSlice({
    name: "user",
    initialState: {
        token: getToken() || "",
        userInfo: {}
    },
    // 同步的修改方法
    reducers: {
        setToken(state, actions) {
            state.token = actions.payload
            N_setToken(actions.payload)
        },
        setUserInfo(state, actions) {
            state.userInfo = actions.payload
        },
        clearUserInfo(state) {
            state.token = ""
            state.userInfo = {}
            removeToken()
        }
    }
})

// 结构出actionCreater
const { setToken, setUserInfo, clearUserInfo } = userStore.actions

// 获取reducer函数
const userReducer = userStore.reducer

export { setToken, setUserInfo, clearUserInfo }

export default userReducer

